<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use app\index\model;

class Syetemmsg extends Common
{
    //添加系统消息
    public function addMessage()
    {
    	$Systemmessage = model('systemmessage');
    	$userId = $request->param("userId");
    	$content = 'erwerwerwrweter';
    	$messageRes = $Systemmessage->insertMessage($userId, $content);
    	echo $messageRes;
    }

    //查询系统消息
    public function listMessage(Request $request)
    {
        $Systemmessage = model('systemmessage');

        $userId = $request->param("userId");


        $listMessage = $Systemmessage->listMessage($userId);

        $listMessage = $this->loop($listMessage);
        echo callback(self::$callback,$listMessage);
    }

    /**
     * 查看已读消息
    */
    public function lookOver(Request $request)
    {
        $Systemmessage = model('systemmessage');

        $userId = $request->param("userId");

        $listMessage = $Systemmessage->listReadMessage($userId);

        $listMessage = $this->loop($listMessage);

        echo callback(self::$callback,$listMessage);
    }

    /**
     * 查看未读消息的详情信息
    */
    public function seeDetails(Request $request)
    {
        $Systemmessage = model('systemmessage');

        $userId = $request->param("userId");

        $messageId = $request->param("messageId");

        // 将该条消息状态值修改为已读
        $updateRes = $Systemmessage->updateStatus($messageId);

        if ($updateRes) 
        {
            $messageInfo = $Systemmessage->seeDetails($messageId);

            $messageInfo['messageTime'] = date('Y-m-d H:i:s',$messageInfo['messageTime']);
            echo callback(self::$callback,$messageInfo);
        }
    }

    // 封装消息列表循环
    public function loop($listMessage)
    {
        foreach ($listMessage as $k => $v) 
        {
            $listMessage[$k]['messageTime'] = date('Y-m-d H:i:s', $v['messageTime']);

            if($v['status'] == 0)
            {
                $listMessage[$k]['status'] = '未读';
            }
            else
            {
                $listMessage[$k]['status'] = '已读';
            }
        }
        return $listMessage;
    }

    // 删除消息记录
    public function deleteMessage(Request $request)
    {
        $Systemmessage = model('systemmessage');

        $userId = $request->param("userId");

        $messageId = $request->param("messageId");


        $deleteRes = $Systemmessage->deleteMessage($messageId);

        if ($deleteRes) 
        {
            echo callback(self::$callback,$deleteRes);
        }
    }
    
}